﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;


namespace WellDone
{
    public partial class EditMenuCategoryDialog : ChildWindow
    {
        public ServiceReferenceOpenRPOS.MenuCategory selectedMenuCategory;

        public EditMenuCategoryDialog()
        {
            InitializeComponent();
        }

        public async void init(string MenuGroup)
        {
            cmbMenuGroup.Items.Clear();
            await Globals.db.GetMenuGroupObjects("Active=1 order by Name");
            if (Globals.db.MenuGroupRecords.Count() > 0)
            {
                foreach (ServiceReferenceOpenRPOS.MenuGroup mg in Globals.db.MenuGroupRecords)
                {
                    cmbMenuGroup.Items.Add(mg.col_Name);
                }
            }
            if (string.IsNullOrEmpty(MenuGroup)==false) cmbMenuGroup.SelectedItem = MenuGroup;
        }

        private async void btnOK_Click(object sender, RoutedEventArgs e)
        {
            if (tbCategoryPrintroute.Text.Trim().Length>0)
            {
                if (Convert.ToInt32(tbCategoryPrintroute.Text.Trim()) > 255)
                {
                    MessageBox.Show("Print Route value should be between 0 - 255");
                }
                else
                {
                    // Update the name of this menu category
                    selectedMenuCategory.col_Name = tbMenuCategoryName.Text.Trim();
                    selectedMenuCategory.bFilterName = true;
                    selectedMenuCategory.col_PrintRoute = Convert.ToByte(tbCategoryPrintroute.Text.Trim());
                    selectedMenuCategory.bFilterPrintRoute = true;
                    selectedMenuCategory.col_Visible = (cbMenuCategoryVisible.IsChecked == true);
                    selectedMenuCategory.bFilterVisible = true;
                    if (cmbMenuGroup.SelectedIndex != -1)
                    {
                        selectedMenuCategory.col_MenuGroupID = Globals.db.MenuGroupRecords[cmbMenuGroup.SelectedIndex].col_ID;
                        selectedMenuCategory.bFilterMenuGroupID = true;
                    }
                    await Globals.db.UpdateMenuCategoryObject(selectedMenuCategory);
                    // Re-init the display
                    Globals.menuItems.initMenuCategories(true);
                    Globals.menuItems.initMenuItems(true);
                    this.DialogResult = true;
                } 
            }
            else
            {
                // Update the name of this menu category
                selectedMenuCategory.col_Name = tbMenuCategoryName.Text.Trim();
                selectedMenuCategory.bFilterName = true;
                selectedMenuCategory.col_Visible = (cbMenuCategoryVisible.IsChecked == true);
                selectedMenuCategory.bFilterVisible = true;
                await Globals.db.UpdateMenuCategoryObject(selectedMenuCategory);
                // Re-init the display
                Globals.menuItems.initMenuCategories(true);
                Globals.menuItems.initMenuItems(true);
                this.DialogResult = true;
            }
           
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void tbMenuCategoryName_KeyDown(object sender, KeyEventArgs e)
        {
            // Call the OK button handlers if "Enter" is pressed
            if (e.Key == Key.Enter)
                btnOK_Click(sender, e);
        }

        private void tbCategoryPrintroute_KeyDown(object sender, KeyEventArgs e)
        {
            e.Handled = !IsNumberKey(e.Key);
        }

        private bool IsNumberKey(Key inKey)
        {
            if (inKey < Key.D0 || inKey > Key.D9)
            {
                if (inKey < Key.NumPad0 || inKey > Key.NumPad9)
                {
                    return false;
                }
            }
            return true;
        }

    }
}

